package log

import (
	"github.com/lestrrat-go/file-rotatelogs"
	"github.com/pkg/errors"
	"github.com/rifflock/lfshook"
	log "github.com/sirupsen/logrus"
	"path"
	"time"
)

//var log = logrus.New()

func InitLogger(logPath string) {
	fileName := time.Now().Format("2006-01-02")
	baseLogPah := path.Join(logPath, fileName)
	writer, err := rotatelogs.New(
		baseLogPah+".log",
		//rotatelogs.WithLinkName(baseLogPah),       // 生成软链，指向最新日志文件
		rotatelogs.WithMaxAge(time.Hour*8760), // 文件最大保存时间
		//rotatelogs.WithRotationCount(365),         // 最多存365个文件
		rotatelogs.WithRotationTime(time.Hour*24), // 日志切割时间间隔
	)
	if err != nil {
		log.Errorf("config local file system logger error. %+v", errors.WithStack(err))
	}
	lfHook := lfshook.NewHook(lfshook.WriterMap{
		log.DebugLevel: writer,
		log.InfoLevel:  writer,
		log.WarnLevel:  writer,
		log.ErrorLevel: writer,
		log.FatalLevel: writer,
		log.PanicLevel: writer,
	}, &log.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	})
	log.AddHook(lfHook)
}

func Info(args ...interface{}) {
	log.Info(args)
}

func Error(args ...interface{}) {
	log.Error(args)
}
